//
//  AppDelegate.swift
//  Sinaweibo
//
//  Created by 谌桃 on 15/11/27.
//  Copyright © 2015年 ryan.chen. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.plist")
        
        print(path)
        
        //实例化window
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
//        window?.rootViewController = defaultRootController()
        
        //单独测试编辑控制器, 避免嵌套的层次结构太多, 测试太繁琐, 也避免频繁刷微博接口, 导致接口被封
        //这样单独测试的好处
        let nav = UINavigationController(rootViewController: ComposeViewController())
        window?.rootViewController = nav
        
        
        window?.makeKeyAndVisible()
        
        window?.backgroundColor = UIColor.whiteColor()
        
        //设置主题色
        setThemeColor()
        
        registerNotification()
        
        return true
    }
    
    func setThemeColor(){
    
        //设置底部item的可视化的颜色, 可以一起设置图片和文字
        UITabBar.appearance().tintColor = themeColor
        
        //设置导航条的可视化item的颜色
        UINavigationBar.appearance().tintColor = themeColor
    }
    
    private func defaultRootController() -> UIViewController{
    
        if UserAccountViewModel().userLogin {
        
            //如果登录了, 就进行欢迎界面
          return  WelcomeViewController()
            
        }else{
        
            //如果没有登录, 就进入主界面
          return  MainViewController()
        }
    }
    
    
    private func registerNotification(){
    
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "switchRootVC:", name: WBSwitchRootVCNotification, object: nil)
    }
    
    @objc private func switchRootVC(n:NSNotification){
    
        window?.rootViewController = n.object == nil ? MainViewController() : WelcomeViewController()
    }

    
    //析构方法中移除通知
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

}

